package cn.com.xy.sms.sdk.provider;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.log.LogManager;
import cn.com.xy.sms.sdk.util.StringUtils;
import cn.com.xy.sms.util.ParseManager;
import cn.com.xy.sms.util.PublicInfoParseManager;
import cn.com.xy.sms.util.SdkCallBack;

/*IMOO-21 likuangkuang 20170104 start */
public class CustomInfoProvider extends ContentProvider {
    private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);

    private static final String AUTHORITY = "cn.com.xy.sms.sdk.provider.custominfo";
    private static final String PATH_PUBLIC_INFO = "publicInfo";
    private static final String PATH_PUBLIC_INFO_LOGO = "publicInfoLogo";

    private static final int CODE_PUBLIC_INFO = 1;

    static {
        MATCHER.addURI(AUTHORITY, PATH_PUBLIC_INFO, CODE_PUBLIC_INFO);
    }

    private static final String[] PROJECTION_PUBLICINFO = { "id", "name", "classifyName", "weiboName", "weiboUrl",
            "weixin", "logo", "logoc", "website" };

    @Override
    public boolean onCreate() {
        return true;
    }

    private Cursor buildCursor(Uri uri, String[] projection, String data) {
        MatrixCursor cursor = new MatrixCursor(projection);
        int length = projection.length;
        Object[] row = new Object[length];
        try {
            JSONObject obj = new JSONObject(data);
            for (int i = 0; i < length; i++) {
                row[i] = obj.opt(projection[i]);
            }

        } catch (Throwable t) {
            LogManager.e(Constant.TAG, "", t);
        }

        cursor.addRow(row);
        cursor.setNotificationUri(getContext().getContentResolver(), uri);
        return cursor;
    }

    private String queryPublicInfo(String selection, String[] selectionArgs) {
        final String[] result = { null };
        if (selectionArgs != null && selectionArgs.length >= 2) {
            String phoneNum = selectionArgs[0];
            String iccid = selectionArgs[1];
            if (!StringUtils.isNull(phoneNum)) {
                Map<String, String> extend = new HashMap<String, String>();
                extend.put("SYNC_QUERY", "");
                result[0] = ParseManager.queryPublicInfo(getContext(), phoneNum, 1, iccid, extend, new SdkCallBack() {

                    @Override
                    public void execute(Object... obj) {
                        if (obj != null && obj.length > 0) {
                            if (!StringUtils.isNull((String) obj[0])) {
                                result[0] = (String) obj[0];
                            }
                        }
                    }
                });
            }
        }

        return result[0];
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        int code = MATCHER.match(uri);
        Cursor cursor = null;
        String result = null;
        String[] outProjection = null;
        switch (code) {
        case CODE_PUBLIC_INFO:
            outProjection = PROJECTION_PUBLICINFO;
            result = queryPublicInfo(selection, selectionArgs);
            break;

        default:
            break;
        }

        if (!StringUtils.isNull(result) && outProjection != null) {
            cursor = buildCursor(uri, outProjection, result);
        }

        return cursor;
    }

    @Override
    public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
        List<String> paths = uri.getPathSegments();
        if (paths == null || paths.size() < 2) {
            throw new FileNotFoundException(uri.getPath());
        }

        String type = paths.get(0);
        File logFile = null;
        if (PATH_PUBLIC_INFO_LOGO.equals(type)) {
            String logoName = paths.get(1);
            logFile = PublicInfoParseManager.findLogoFile(logoName);
        }

        if (logFile != null && logFile.exists()) {
            return ParcelFileDescriptor.open(logFile, ParcelFileDescriptor.MODE_READ_ONLY);
        }

        throw new FileNotFoundException(uri.getPath());
    }

    @Override
    public String getType(Uri uri) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        // TODO Auto-generated method stub
        return 0;
    }
}
/* IMOO-21 likuangkuang 20170104 end */